package com.itjiange.dao;

import com.itjiange.modle.Dept;
import com.itjiange.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

public class DeptDao {
    /**
     * 查询所有部门
     * @return
     * @throws SQLException
     */
    public List<Dept> selectAllDept() {
        QueryRunner queryRunner = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from dept";
        try {
            return queryRunner.query(sql,new BeanListHandler<>(Dept.class));
        } catch (SQLException e) {
            e.printStackTrace();
            return Collections.emptyList();
        }
    }

    /**
     * 根据 id 删除部门
     * @param deptId
     * @return
     */
    public int delete(int deptId) {
        QueryRunner queryRunner = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "delete from dept where deptId = ?";
        try {
            return queryRunner.update(sql,deptId);
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }
}
